package com.sykj.cloud.system.controller;

import com.sykj.cloud.common.core.constant.UserConstants;
import com.sykj.cloud.common.core.domain.R;
import com.sykj.cloud.common.core.utils.StringUtils;
import com.sykj.cloud.common.core.utils.poi.ExcelUtil;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.core.web.page.TableDataInfo;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;
import com.sykj.cloud.common.security.utils.SecurityUtils;
import com.sykj.cloud.system.api.domain.Member;
import com.sykj.cloud.system.api.model.LoginMember;
import com.sykj.cloud.system.service.IMemberService;
import com.sykj.cloud.system.service.ISysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 会员Controller
 *
 * @author mr.will
 * @date 2021-03-16
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController
{
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ISysPermissionService permissionService;

    /**
     * 新增会员
     */
    @PreAuthorize(hasPermi = "system:member:add")
    @Log(title = "会员", logUserType = LogUserType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member)
    {
        if (member==null)
        {
            return AjaxResult.error("新增会员失败，会员不能为空");
        }
        else if (StringUtils.isEmpty(member.getLoginName()))
        {
            return AjaxResult.error("新增会员'" + member.getName() + "'失败，登录名称不能为空");
        }
        else if (StringUtils.isEmpty(member.getPassword()))
        {
            return AjaxResult.error("新增会员'" + member.getName() + "'失败，登录密码不能为空");
        }else if (UserConstants.NOT_UNIQUE.equals(memberService.checkMemberLoginName(member)))
        {
            return AjaxResult.error("新增会员'" + member.getName() + "'失败，登录账号已存在");
        }
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword()));
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 获取当前会员信息
     */
    @GetMapping("/info/{loginname}")
    public R<LoginMember> info(@PathVariable("loginname") String loginname)
    {
        Member member = memberService.selectMemberrByLoginName(loginname);
        if (StringUtils.isNull(member))
        {
            return R.fail("登录名或密码错误");
        }
        LoginMember loginMember = new LoginMember();
        loginMember.setMember(member);
        loginMember.setPermissions(permissionService.getMenuPermissionList());
        return R.ok(loginMember);
    }

    /**
     * 新增会员
     */
    @PostMapping("/add")
    public AjaxResult addHttp(@RequestBody Member member)
    {
        if (member==null)
        {
            return AjaxResult.error("新增会员失败，会员不能为空");
        }
        else if (StringUtils.isEmpty(member.getLoginName()))
        {
            return AjaxResult.error("新增会员'" + member.getName() + "'失败，登录名称不能为空");
        }
        else if (StringUtils.isEmpty(member.getPassword()))
        {
            return AjaxResult.error("新增会员'" + member.getName() + "'失败，登录密码不能为空");
        }else if (UserConstants.NOT_UNIQUE.equals(memberService.checkMemberLoginName(member)))
        {
            return AjaxResult.error("新增会员'" + member.getName() + "'失败，登录账号已存在");
        }
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword()));
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改会员
     */
    @PreAuthorize(hasPermi = "system:member:edit")
    @Log(title = "会员", logUserType = LogUserType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member)
    {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除会员
     */
    @PreAuthorize(hasPermi = "system:member:remove")
    @Log(title = "会员", logUserType = LogUserType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(memberService.deleteMemberByIds(guids));
    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize(hasPermi = "system:member:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(memberService.getMemberById(guid));
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize(hasPermi = "system:member:export")
    @Log(title = "会员", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Member member) throws IOException
    {
        List<Member> list = memberService.listMembers(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        util.exportExcel(response, list, "member");
    }

    /**
     * 查询会员列表
     */
    @PreAuthorize(hasPermi = "system:member:list")
    @GetMapping("/list")
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.listMembers(member);
        return getDataTable(list);
    }

    /**
     * 查询站点信息，下拉框值
     */
    @PreAuthorize(hasPermi = "system:member:list")
    @GetMapping("/list/options")
    public AjaxResult optionList(Member member)
    {
        return AjaxResult.success(memberService.listMembers(member));
    }
}
